﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Helena.Areas.Vendedores.Models;
using Telerik.Web.Mvc;

namespace Helena.Areas.Vendedores.Controllers
{
    public class VendedorController : Controller
    {
        LogicaVendedor db = new LogicaVendedor();

        public ActionResult Index()
        {
            VendedorView vw = (VendedorView)Session["Vendedor"];
            try
            {
                if (vw.ID != 0)
                {
                    if (vw.admin)
                    {
                        return View("IndexVendedor");
                    }
                    else
                    {
                        return Redirect("../Home/Error");
                    }
                }
                else
                {
                    return Redirect("../Home/NoSession");
                }
            }
            catch
            {
                return Redirect("../Home/NoSession");
            }
            
        }

        [GridAction]
        public ActionResult Select()
        {
            return View("Index", new GridModel(db.i_RetornarVendedores()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Insert()
        {

            VendedorView v_v = new VendedorView();
            if (TryUpdateModel(v_v))
            {
                v_v.NombreLargo = v_v.Nombre + " - " + v_v.apaterno;
                v_v.Estado = true;
                db.i_AgregarVendedor(v_v);
            }
            return View("Index", new GridModel(db.i_RetornarVendedores()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Delete(int? id)
        {
            int ID = id ?? -1;
            db.i_EliminarLogicoVendedor(ID);
            return View("Index", new GridModel(db.i_RetornarVendedores()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Update(VendedorView v)
        {
            v.Estado = true;
            db.i_ModificarVendedor(v);
            return View("Index", new GridModel(db.i_RetornarVendedores()));
        }
    }
}
